package com.xh.petdogexpert.sys.controller;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.xh.petdogexpert.base.BaseController;
import com.xh.petdogexpert.base.BaseException;
import com.xh.petdogexpert.base.ResponseData;
import com.xh.petdogexpert.sys.dto.Lov;
import com.xh.petdogexpert.sys.service.ILovService;

@RequestMapping(value = "sys/")
@Controller
public class LovController extends BaseController{

	@Autowired
	ILovService lovService;

	final static Logger logger = LoggerFactory.getLogger(LovController.class);

	@RequestMapping(value = "lov/queryLovs")
	@ResponseBody 
	public ResponseData queryLovs(Lov lov) {

		ResponseData rData = new ResponseData();
		List<Lov> lovs = lovService.queryLovs(lov);
		rData.setData(lovs);
		return rData;
	}

	@RequestMapping(value = "lov/getLov")
	@ResponseBody
	public ResponseData getLov(Lov lov) throws BaseException {

		ResponseData rData = new ResponseData();
		Lov rLov = null;

		rLov = lovService.getLov(lov);
		rData.setData(rLov);

		return rData;
	}

	@RequestMapping(value = "lov/addUpdateLov")
	@ResponseBody
	public ResponseData addUpdateLov(String lovsStr) throws BaseException {

		ObjectMapper objectMapper = new ObjectMapper();
		JavaType javaType = objectMapper.getTypeFactory().constructParametricType(ArrayList.class, Lov.class);
		List<Lov> lovs;

		try {
			lovs = objectMapper.readValue(lovsStr, javaType);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new BaseException("转换出错");
		}

		ResponseData rData = new ResponseData();

		lovService.addUpdateLov(lovs);

		rData.setMsg("保存成功");

		return rData;
	}

	@RequestMapping(value = "lov/deleteLov")
	@ResponseBody
	public ResponseData deleteLov(Lov lov) throws BaseException {

		ResponseData rData = new ResponseData();

		int count = lovService.deleteLov(lov);
		rData.setData(count);
		rData.setMsg("删除成功");
		return rData;
	}
 
}
